(function () {
    var inputs = document.getElementsByTagName('input');
    for (var i = 0; inputs[i]; i++) inputs[i].autocomplete = 'off';

    //打开日期选择
    $(document).on('click', '.input-text.date-ico', function(e) {
        var target = e.target || e.srcElement;
        var _t = $(target);
        if (_t.hasClass('disabled')) return;
        var showclear = (typeof _t.attr("isHideClear") == 'undefined')?true: false;//isShowClear:false
        // console.log($dp);
        if ($('.animate-show').length > 0 && _t.hasClass('datepickopen')) {
            _t.removeClass('datepickopen');
            $dp.hide();
            return;
        }
        _t.addClass('datepickopen');
        var defaultOpt = {};
        // defaultOpt.skin = "orange";
        defaultOpt.el = target;
        defaultOpt.isShowClear = showclear;
        defaultOpt.readOnly = true;
        defaultOpt.onpicked = function() {
            _t.removeClass('datepickopen');
        };
        defaultOpt.oncleared = function() {
            _t.removeClass('datepickopen');
            _t.trigger('datechanged');
        };
        var opt = _t.attr('data-options');
        if (!opt) {
            WdatePicker(defaultOpt);
            return;
        }
        var opts = opt.split(',').length > 0 ? opt.split(',') : [];
        var optis = {};
        $.each(opts, function(i, a) {
            if (a.split(':').length > 1) {
                optis[a.split(':')[0]] = a.split(':')[1];
            }
        });

        //显示开始时间不大于结束时间
        if (typeof optis['maxDate'] !='undefined') {
            defaultOpt.maxDate = '#F{$dp.$D(\''+optis['maxDate']+'\')}';
        }
        if (typeof optis['minDate'] !='undefined') {
            defaultOpt.minDate = '#F{$dp.$D(\''+optis['minDate']+'\')}';
        }
        
        if (_t.attr('date-type') && _t.attr('date-type') == 'month') {
            defaultOpt.dateFmt = 'yyyy-MM';
            WdatePicker(defaultOpt);
            return;
        }

        var seconds = optis['seconds'] ? eval(optis['seconds'].toLowerCase()) : false;
        if (seconds) {
            defaultOpt.dateFmt = 'yyyy-MM-dd HH:mm';
        }
        if (optis['vdate']) {
            if (optis['vdate'] == 'today') {
                var vdtime = _t.attr('data-vkey');
                defaultOpt.opposite = true;
                defaultOpt.disabledDates = [seconds ? vdtime.substring(0, 10) : vdtime];
                defaultOpt.onpicked = inputdatechange;
            }
            if (optis['vdate'] == 'beforeVtime') {
                var vdtime = _t.attr('data-vkey');
                var vmintime = _t.attr('data-minkey');
                vmintime = vmintime ? vmintime : '';
                if (_t.attr('data-minkey') != '' && Core.rule('datetime', vmintime)) {
                    defaultOpt.minDate = _t.attr('data-minkey').substring(0,10) + " 00:00:00";
                }
                defaultOpt.maxDate = vdtime!=''?(vdtime.substring(0,10) + " 23:59:59"):vdtime;
                defaultOpt.onpicked = inputdatechange;
                // console.log(defaultOpt);
            }
        }

        //显示开始时间不大于结束时间
        if (typeof optis['maxDate'] !='undefined') {
            defaultOpt.maxDate = '#F{$dp.$D(\''+optis['maxDate']+'\')}';
        }
        if (typeof optis['minDate'] !='undefined') {
            defaultOpt.minDate = '#F{$dp.$D(\''+optis['minDate']+'\')}';
        }

        function inputdatechange() {
            _t.removeClass('datepickopen');

            // console.log($dp.cal.getDateStr(), _t.attr('data-vkey'));
            if ($dp.cal.getDateStr() != _t.attr('data-vkey')) {
                _t.val($dp.cal.getDateStr());
                _t.attr('changed', true);
            } else {
                _t.removeAttr('changed');
            }
            _t.trigger('datechanged');
        }
        WdatePicker(defaultOpt);
    });

    //tips 提示
    $('.cover-tips').on('mouseenter',function(){
        var _t = $(this);
        var _style = typeof _t.attr('tip-style')!="undefined"?_t.attr('tip-style'):"";
        console.log(_style);
        var _t_top = _t.offset().top;
        var _c = _t.find('.c-tips');
        if(_t.find('.c-tips').length === 0){
            _t.append('<div class="c-tips" style="'+_style+'">'+(_t.attr('fonts')||'&nbsp;')+'</div>');
            _c = _t.find('.c-tips');
        }
        if(_c.outerHeight()>_t_top){
            _t.addClass('pos-bottom');
        }
        _c.css({
            top: _c.outerHeight()>_t_top? 'auto':(-_c.outerHeight()-10)+'px',
            bottom: _c.outerHeight()>_t_top? (-_c.outerHeight()-10)+'px':'auto'
        }).show();
    }).on('mouseleave',function(){
        var _t = $(this);
        _t.removeClass('pos-bottom');
        var _c = _t.find('.c-tips');
        if(_c){
            _c.hide();
        }
    });

    //关闭弹出层
    $.fn.extend({
        dialogClose: function() {
            if (window !== top) {
                $('.layui-layer-shade', parent.document).fadeOut().remove();
                $('.layui-layer-move', parent.document).fadeOut().remove();
                $('.layui-layer-iframe', parent.document).fadeOut().remove();
            }
            var _this = $(this);
            if (_this.parents('.layui-layer-page').length > 0) {
                _this.parents('.layui-layer-page').find('.layui-layer-close')[0].click();
            }
        }
    });
    $(document).on('click', '.layui-layer-page a[href="#close"],.dialog_box a[href="#close"],form[form-role="dialog"] button[role="btn-cancel"]', function(e) {
        e.preventDefault();
        if (window !== top) {
            $('.layui-layer-shade', parent.document).fadeOut().remove();
            $('.layui-layer-move', parent.document).fadeOut().remove();
            $('.layui-layer-iframe', parent.document).fadeOut().remove();
        }
        $(this).parents('.layui-layer-page').find('.layui-layer-close')[0].click();
    });

    //tab
    $.fn.extend({
        loadtab:function(){
            var $t = this;
            if($t.find('.tabs-link-wrap').length===0 || $t.find('.tabs-box').length===0){
                return;
            }
            $t.find('.tabs-link-wrap a').off('click').on('click',function(e){
                var _t = $(this);
                e.preventDefault();
                _t.addClass('active').siblings('a').removeClass('active');
                $t.find('.tabs-box').addClass('disn');
                $(_t.attr('href')).removeClass('disn');
            })
            
        }
    });

})();

